/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Class for Sending Mail
*  Name: Sendmail.java
*  Author: www.pecats.com
*  Date: Sep 15, 2008
************************************************************/

package com.pecats.batch.core;

import java.util.Properties;

import java.io.File;
import java.io.FileInputStream;

import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.BodyPart;
import javax.mail.Session;
import javax.mail.Multipart;
import javax.mail.Authenticator;
import javax.mail.Message;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.pecats.batch.util.ParseXMLFile;
import com.pecats.batch.data.ConfigData;
import com.pecats.batch.main.openXplore;

/**
 * Used to send reports to the customer via email
 */

public class Sendmail extends Authenticator
{

	static Logger logger1 = Logger.getLogger(Sendmail.class.getName());
	String username;
	String password;

	public Sendmail()
	 {
		ParseXMLFile parseFile = new ParseXMLFile();
		parseFile.parseXML(openXplore.CONFIGDIR + "\\" + openXplore.CONFIGFILE);
	 }

	public void send(File aFile, File aDir)
	{
	
		SecurityManager security = System.getSecurityManager();
		try
		{
			File propFile = new File(openXplore.CONFIGDIR + "\\" + ConfigData.mailproperties);

			// Get system properties
			Properties props = new Properties();   		
			props.load(new FileInputStream(propFile));

	        	// Get session
			username = (String) props.get("mail.smtp.username");
			password = (String) props.get("mail.smtp.password");

			Authenticator auth = new SMTPAuthenticator();
			Session session = Session.getInstance(props, auth);
 
			// Create an "Alternative" Multipart message
			Multipart mp = new MimeMultipart("alternative");

			String htmlfile = ConfigData.outputdir + "\\" + aDir.getName() + "\\" + aFile.getName() + "main.html";
			BodyPart bp2 = getFileBodyPart(htmlfile);
			mp.addBodyPart(bp2);

			session.setDebug(true);
			MimeMessage msg = new MimeMessage(session);

			msg.setSubject("OpenXplore - Your Web Analytics Results");
			msg.setContent(mp);
	
			msg.setFrom(new InternetAddress((String)props.get("mail.smtp.user")));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(aDir.getName()));
			Transport.send(msg);
		}
		catch (Exception mex)
		{
			mex.printStackTrace();
		}
	}

   	public BodyPart getFileBodyPart(String filename)
      			throws javax.mail.MessagingException
	{
	   BodyPart bp = new MimeBodyPart();
         bp.setDataHandler(new DataHandler
       	     (new FileDataSource(filename)));
         return bp;
       }

	private class SMTPAuthenticator extends javax.mail.Authenticator
	{
		public PasswordAuthentication getPasswordAuthentication()
		{
			return new PasswordAuthentication(username, password);
		}
	}
}